package com.cqan.codec;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cqan.lang.ByteUtils;

public class CodecUtils {

	public static String decodeUnicode(final String dataStr) {
		final StringBuffer buffer = new StringBuffer();
		Pattern p = Pattern.compile("\\\\u([\\S]{4})([^\\\\]*)");
		Matcher match = p.matcher(dataStr);
		while (match.find()) {
			char letter = (char) Integer.parseInt(match.group(1), 16);
			buffer.append(new Character(letter).toString());
			buffer.append(match.group(2));
		}
		return buffer.toString();
	}

	/**
	 * 对字符串做MD5处理
	 * @param str
	 * @return 返回MD5值
	 */
	public static String md5(String str) {
		try {
			MessageDigest alga = MessageDigest.getInstance("MD5");
			alga.update(str.getBytes());
			byte[] digesta = alga.digest();
			return ByteUtils.byte2hex(digesta);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
}
